package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {



    @Autowired
    private EmpMapper empMapper;

//    @Override
//    public List<Dept> findAll() {
//        //return deptMapper.selectList(null);
//        return list();
//    }
//
   @Override
    public void deleteById(Integer id) {
        //1. 判断部门下是否有员工， 如果有， 需要提示错误信息
       LambdaQueryWrapper<Emp> queryWrapper=new LambdaQueryWrapper<>();
       queryWrapper.eq(Emp::getDeptId,id);
       Long count = empMapper.selectCount(queryWrapper);
       if(count > 0){
            throw new RuntimeException("部门下有员工， 不能删除");
        }

        this.removeById(id);
    }
//
//    @Override
//    public void add(Dept dept) {
//        //1. 补全基础属性 - createTime, updateTime
//        dept.setCreateTime(LocalDateTime.now());
//        dept.setUpdateTime(LocalDateTime.now());
//
//        //2. 调用Mapper接口方法插入数据
//        //deptMapper.insert(dept);
//        save(dept);
//    }
//
//    @Override
//    public Dept getDeptById(Integer id) { //这个方法需要改名，不然会跟MP提供的方法冲突，接口也要改
//        //return deptMapper.selectById(id);
//        Dept dept = getById(id);
//        return dept;
//    }


}